const express = require('express')
const app = express()

const request = require('request')

app.set('view engine', 'ejs')
app.set('views', './ejs_pages')

// 没有挂载路径的中间件，应用中的每个请求都会执行该中间件
app.use((req,res,next) => {
    console.log('Time',Date.now());
    next();
})

var person1 = {}
app.get('/', function(req, res, next) {
    // 请求地址https://h5.133.cn/hangban/basic/search_airport?q=%E5%8C%97%E4%BA%AC
    request('https://h5.133.cn/hangban/basic/search_airport?q=%E5%8C%97%E4%BA%AC',function(error,response,body){
        /*判断请求是否成功*/
        if (!error && response.statusCode == 200) {
            /*把字符串转换为jsons数组*/
            person1 = JSON.parse(body);
            /*渲染模板*/
            // console.log(body);
            // console.log(typeof body)
            person1.forEach(item => {
                item.nation = 'china';
            });
            res.render('index.ejs',  {person1} );
        }
    });
});

app.listen(3000, () => {
    console.log('server running at http://127.0.0.1:3000')
})
